//
//  GuideViewController.m
//  ZPGYApp
//
//  Created by Pro.
//
//

#import "GuideViewController.h"
#import "AppConfig.h"

@interface GuideViewController ()
{
    NSArray *imageList;
}

@end

@implementation GuideViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
	// Do any additional setup after loading the view.
//    [[UIApplication sharedApplication] setStatusBarHidden:YES];
    [self initIntroduceImages];
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)viewDidUnload {
    [self setBackgroundView:nil];
    [self setScrollView:nil];
    [self setPageControl:nil];
    [super viewDidUnload];
}

- (void) initIntroduceImages
{
    self.scrollView.showsHorizontalScrollIndicator = NO;
    self.scrollView.showsVerticalScrollIndicator = NO;
    self.scrollView.scrollEnabled = YES;
    self.scrollView.pagingEnabled = YES;
    self.scrollView.userInteractionEnabled = YES;
    //    if (IS_IPHONE_5) {
    //        imageList = [NSArray arrayWithObjects:@"ydy_1_5.png", @"ydy_2_5.jpg", @"ydy_3_5.jpg", @"ydy_4_5.jpg", nil];
    //
    //    } else {
    //        imageList = [NSArray arrayWithObjects:@"ydy_1.png", @"ydy_2.jpg", @"ydy_3.jpg", @"ydy_4.jpg", nil];
    //
    //    }
    imageList = [NSArray arrayWithObjects:@"guidePageImg1.png", @"guidePageImg2.jpg", @"guidePageImg3.jpg", nil];
    
    
    self.pageControl.numberOfPages = imageList.count;
    self.pageControl.currentPage = 0;
    
    [self addImageViewToScrollView];
}

- (void) addImageViewToScrollView
{
    CGRect screenBounds = [UIScreen mainScreen].bounds;
    
    UIColor* c1 = GET_COLOR(0, 87, 114);
    UIColor* c2 = GET_COLOR(0, 48, 59);
    if(imageList)
    {
        [self.scrollView setContentSize:CGSizeMake(self.scrollView.frame.size.width * [imageList count], screenBounds.size.height)];
        for(int i = 0;i<imageList.count;i++)
        {
            // 底层
            UIView* bottomView = nil;
            if(IS_IPHONE_5)
            {
                bottomView = [[UIView alloc] initWithFrame:CGRectMake(i*screenBounds.size.width, 88, screenBounds.size.width, screenBounds.size.height)];
            }
            else
            {
                bottomView = [[UIView alloc] initWithFrame:CGRectMake(i*screenBounds.size.width, 24, screenBounds.size.width, screenBounds.size.height+64)];
            }
            
            // 添加图片
            UIImage* image = [UIImage imageNamed:[imageList objectAtIndex:i]];
            UIImageView* imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, image.size.width, image.size.height)];
            [imageView setImage:image];
            [bottomView addSubview:imageView];
            
            // 设置颜色
            if(i == 0 || i == 3)
            {
                bottomView.backgroundColor = c1;
            }
            else
            {
                bottomView.backgroundColor = c2;
            }
            
            // 加入
            [self.scrollView addSubview:bottomView];
            bottomView.clipsToBounds = YES;
            bottomView.userInteractionEnabled = YES;
            
            if(i == imageList.count-1)
            {
                // 按钮
                UIImage* btnNomalIamge = [UIImage imageNamed:@"guidePageBtnNormal"];
                UIImage* btnChooseImage = [UIImage imageNamed:@"guidePageBtnClick"];
                float btnx = (screenBounds.size.width-btnNomalIamge.size.width)/2;
                float btny = 0;
                if(IS_IPHONE_5)
                {
                    btny = (screenBounds.size.height-btnNomalIamge.size.height)/1.17;
                }
                else
                {
                    btny = (screenBounds.size.height-btnNomalIamge.size.height)/0.98;
                }
                
                UIButton* btn = [UIButton buttonWithType:UIButtonTypeCustom];
                [btn setBackgroundImage:btnNomalIamge forState:UIControlStateNormal];
                [btn setBackgroundImage:btnChooseImage forState:UIControlStateSelected];
                [btn addTarget:self action:@selector(gotoMainPage) forControlEvents:UIControlEventTouchUpInside];
                [bottomView addSubview:btn];
                
                btn.frame = CGRectMake(btnx, btny, btnNomalIamge.size.width, btnNomalIamge.size.height);
                
            }
        }
    }
}

#pragma mark - UIScrollViewDelegate

- (void)scrollViewDidEndDecelerating:(UIScrollView *) mscrollView
{
    NSInteger currentIndex = mscrollView.contentOffset.x / self.view.frame.size.width;
    self.pageControl.currentPage = currentIndex;
}

#pragma mark - 按钮事件
- (void)gotoMainPage
{
    // Go to Index
//    [[UIApplication sharedApplication] setStatusBarHidden:NO];
    
    [self dismissViewControllerAnimated:YES completion:^{
        [GuideViewController setShowed];
    }];
}

+ (BOOL)shouldShow
{
    NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults];
    BOOL showed = [prefs boolForKey:@"guide_showed"];
    
    return !showed;
}

+ (void)setShowed
{
    NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults];
    [prefs setBool:YES forKey:@"guide_showed"];
    [prefs synchronize];
}

@end
